package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhentao.mapper.CdzUserCouponMapper;
import com.zhentao.model.common.dtos.PageResponseResult;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.dtos.WdYhjDto;
import com.zhentao.model.common.pojo.coupon.CdzUserCoupon;
import com.zhentao.model.common.pojo.coupon.cdzdtos.ComputeAmountDto;
import com.zhentao.model.common.pojo.coupon.cdzdtos.CouponDTO;
import com.zhentao.service.ICdzUserCouponService;
import com.zhentao.userLocal.UserThreadLocalUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mp
 * @since 2024-12-18
 */
@Service
public class CdzUserCouponServiceImpl extends ServiceImpl<CdzUserCouponMapper, CdzUserCoupon> implements ICdzUserCouponService {
    @Resource
        private CdzUserCouponMapper cdzUserCouponMapper;
    @Override
    /**
     * @description:分页的用户优惠卷列表
     * @author: muhaoyang
     * @date: 2024/12/26 13:49
     * @param: [couponDTO]
     * @return: com.zhentao.model.common.dtos.ResponseResult
     **/
    public ResponseResult listwd(CouponDTO couponDTO) {
//        获取用户id
        Long userId = UserThreadLocalUtil.getUser();
//        设置分页参数
        PageHelper.startPage(couponDTO.getPageNum(),couponDTO.getPageSize());
//        执行查询
        List<WdYhjDto> list = cdzUserCouponMapper.listwd(userId);
//        执行分页
        PageInfo<WdYhjDto> pageInfo = new PageInfo<>(list);
//        返回前端
        PageResponseResult pageResponseResult = new PageResponseResult(couponDTO.getPageNum(),couponDTO.getPageSize(),(int)pageInfo.getTotal());
        pageResponseResult.setData(pageInfo.getList());

        return pageResponseResult;
    }

    @Override
    /*
     * @description:计算优惠后的金额
     * @author: muhaoyang
     * @date: 2024/12/26 15:27
     * @param: [computeAmountDto]
     * @return: com.zhentao.model.common.dtos.ResponseResult
     **/
    public ResponseResult computeAmount(ComputeAmountDto computeAmountDto) {
//        总金额(用户输入的金额)
        Integer amount = Integer.valueOf(computeAmountDto.getAmount());
//        执行查询
        Integer integer = cdzUserCouponMapper.selectfaceValueMany(computeAmountDto.getSelectedCouponIds());
//        用户金额减去优惠金额
        if (amount-integer>=0){
            return ResponseResult.okResult(amount-integer);
        }
        return null;
    }

    @Override
    /**
     * @description:删除用户优惠卷关联关系
     * @author: muhaoyang
     * @date: 2024/12/26 21:36
     * @param: [couponIds, userId]
     * @return: com.zhentao.model.common.dtos.ResponseResult
     **/
    public ResponseResult deleteUserCoupon(List<Integer> couponIds, Long userId) {

        for (Integer couponId : couponIds) {
//            获取单条信息
            CdzUserCoupon userCoupon = this.getOne(Wrappers.<CdzUserCoupon>lambdaQuery().eq(CdzUserCoupon::getUserId, userId).eq(CdzUserCoupon::getCouponId, couponId));
//            执行删除
            userCoupon.setStatus(2);
            this.updateById(userCoupon);
        }
        return ResponseResult.okResult("删除成功");
    }
}
